package com.qfedu.mtl.controller;

import com.alibaba.fastjson.JSON;
import com.qfedu.mtl.entity.User;
import com.qfedu.mtl.service.UserService;
import com.qfedu.mtl.service.impl.UserServiceImpl;
import com.qfedu.mtl.util.CharacterUtil;
import com.qfedu.mtl.util.HttpUtil;
import com.qfedu.mtl.util.StrUtil;
import com.qfedu.mtl.util.page.Pager;
import com.qfedu.mtl.util.web.annotation.Controller;
import com.qfedu.mtl.util.web.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @version v1.0
 * @Date 2023/9/25 11:24
 * @Author Kyi
 * @Email zywKyi@163.com
 */

@Controller
@RequestMapping("/user")
public class UserController {
    User user = new User();
    UserService userService = new UserServiceImpl();

    //增加
    @RequestMapping("/add")
    public void add(HttpServletRequest req, HttpServletResponse response) throws IOException {
        CharacterUtil.setUtf8(req, response);

        try {
            userService.add(user);
            HttpUtil.response(null,0, "成功", response);

        } catch (Exception e) {
            HttpUtil.response(null,-1, "失败", response);
        }


    }

    //删除
    @RequestMapping("/delete")
    public void delete(HttpServletRequest req, HttpServletResponse response) throws IOException {
        CharacterUtil.setUtf8(req, response);

        String id = req.getParameter("id");
        try {
            userService.delete(Integer.valueOf(id));
            HttpUtil.response(null,0, "成功", response);

        } catch (Exception e) {
            HttpUtil.response(null,-1, "失败", response);
        }

    }

    //    修改
    @RequestMapping("/update")
    public void update(HttpServletRequest req, HttpServletResponse response) throws IOException {
        CharacterUtil.setUtf8(req, response);


        try {
            userService.update(user);
            HttpUtil.response(null,0, "成功", response);
        } catch (Exception e) {
            HttpUtil.response(null,-1, "失败", response);
        }

    }

    //查找一个用户
    @RequestMapping("/find")
    public void findById(HttpServletRequest req, HttpServletResponse response) throws IOException {
        CharacterUtil.setUtf8(req, response);
        String id = req.getParameter("id");

        try {
            User user = userService.findById(Integer.valueOf(id));
            HttpUtil.response(null,0, "成功", response);
        } catch (Exception e) {
            HttpUtil.response(null,-1, "失败", response);
        }


    }

    //条件查询及分页
    @RequestMapping("/findAll")
    public void findAllPage(HttpServletRequest req, HttpServletResponse response) throws IOException {
        CharacterUtil.setUtf8(req, response);
        CharacterUtil.setContentWithJson(req, response);
        String page = req.getParameter("page");
        String size = req.getParameter("size");

        String pag = StrUtil.ifNull(page, "1");
        String siz = StrUtil.ifNull(size, "10");
        String username = req.getParameter("username");

//        UserQuery query = HttpUtil.request2Bean(req, UserQuery.class);


        Pager allPage = userService.findAllPage(Integer.valueOf(pag), Integer.valueOf(siz), username);
        if (allPage != null) {
            Map<String,Object> vo = new HashMap<>();
            vo.put("code",0);
            vo.put("msg","成功");
            vo.put("data",allPage.getRows());
            response.getWriter().println(JSON.toJSONString(vo));

        } else {
            HttpUtil.response(null,-1, "失败", response);
        }

    }

}
